<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Calculator</title>
    <style>
        #calculator {
            width: 200px;
            margin: 100px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        input[type="text"] {
            width: 100%;
            margin-bottom: 10px;
            padding: 5px;
        }
        button {
            width: 45%;
            margin: 2.5%;
            padding: 10px;
            font-size: 16px;
        }
    </style>
</head>
<body>

<div id="calculator">
    <input type="text" id="display" disabled>
    <br>
    <button onclick="appendNumber('1')">1</button>
    <button onclick="appendNumber('2')">2</button>
    <button onclick="appendNumber('3')">3</button>
    <button onclick="setOperation('+')">+</button>
    <br>
    <button onclick="appendNumber('4')">4</button>
    <button onclick="appendNumber('5')">5</button>
    <button onclick="appendNumber('6')">6</button>
    <button onclick="setOperation('-')">-</button>
    <br>
    <button onclick="appendNumber('7')">7</button>
    <button onclick="appendNumber('8')">8</button>
    <button onclick="appendNumber('9')">9</button>
    <button onclick="setOperation('×')">×</button>
    <br>
    <button onclick="appendNumber('0')">0</button>
    <button onclick="clearDisplay()">C</button>
    <button onclick="calculateResult()">=</button>
    <button onclick="setOperation('÷')">÷</button>
</div>

<script>
    let currentNumber = '';
    let previousNumber = '';
    let operation = null;

    function appendNumber(number) {
        currentNumber += number;
        updateDisplay();
    }

    function setOperation(op) {
        if (currentNumber === '') return;
        if (previousNumber !== '') {
            calculateResult();
        }
        operation = op;
        previousNumber = currentNumber;
        currentNumber = '';
    }

    function calculateResult() {
        let result;
        const prev = parseFloat(previousNumber);
        const current = parseFloat(currentNumber);
        if (isNaN(prev) || isNaN(current)) return;

        switch (operation) {
            case '+':
                result = prev + current;
                break;
            case '-':
                result = prev - current;
                break;
            case '×':
                result = prev * current;
                break;
            case '÷':
                result = current !== 0 ? prev / current : 'Error';
                break;
            default:
                return;
        }
        currentNumber = result.toString();
        operation = undefined;
        previousNumber = '';
        updateDisplay();
    }

    function clearDisplay() {
        currentNumber = '';
        previousNumber = '';
        operation = undefined;
        updateDisplay();
    }

    function updateDisplay() {
        document.getElementById('display').value = currentNumber;
    }
</script>

</body>
</html>